package com.patrrr.Widget;

import com.patrrr.Note.NoteBean;
import com.patrrr.Note.NoteEditor;
import com.patrrr.Note.NoteList;
import com.patrrr.Note.R;
import com.patrrr.SQLite3.DBHandler;
import com.patrrr.app.Background;
import com.patrrr.app.NoteInfo;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;

public class NoteWidget extends AppWidgetProvider {

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		System.out.println("widgetIds--" + appWidgetIds.length);
		NoteBean noteBean = null;
		DBHandler dbHandler = new DBHandler(context);
		for (int i = 0; i < appWidgetIds.length; i++) {
			noteBean = dbHandler.getNoteBean(appWidgetIds[i]);
			if (noteBean != null) {
				WidgetUtil.updateWidget(context, appWidgetManager, noteBean,
						NoteEditor.class);
			} else {
				Intent intent = new Intent(context, NoteList.class);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				RemoteViews views = new RemoteViews(context.getPackageName(),
						R.layout.widget);
				views.setTextViewText(R.id.widget_text, context
						.getString(R.string.widget_contentHasBeenDeleted));
				views
						.setOnClickPendingIntent(R.id.widget_layout,
								pendingIntent);
				appWidgetManager.updateAppWidget(appWidgetIds[i], views);
			}
		}
		dbHandler.close();
	}

	public static class WidgetUtil {
		public static void updateWidget(Context context){
			AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);
			appWidgetManager.updateAppWidget(new ComponentName(context, NoteEditor.class), null);
		}

		public static void updateWidget(Context context,
				AppWidgetManager appWidgetManager, NoteBean noteBean,
				Class<?> cls) {
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.widget);
			if (!(noteBean.getContent().equals(""))
					&& noteBean.getContent() != null) {
				Intent intent = new Intent(context, cls);
				Bundle bundle = new Bundle();
				bundle.putSerializable(NoteInfo.NOTE_BEAN, noteBean);
				intent.putExtras(bundle);
				intent.setAction(noteBean.getWidgetId()
						+ NoteEditor.MODE_VIEW_WIDGET);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				views.setTextViewText(R.id.widget_text, noteBean.getContent());
				views.setImageViewResource(R.id.widget_image,
						Background.widget_bg_small[noteBean.getColor()]);
				views
						.setOnClickPendingIntent(R.id.widget_layout,
								pendingIntent);
			} else {
				Intent i = new Intent(context,NoteList.class);
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
				views.setTextViewText(R.id.widget_text, context
						.getString(R.string.widget_contentHasBeenDeleted));
				views
						.setOnClickPendingIntent(R.id.widget_layout,
								pendingIntent);
			}
			appWidgetManager.updateAppWidget(noteBean.getWidgetId(), views);
		}
	}
}
